<template>
  <div class="miss">
    <div class="title">
      <p>重设密码</p>
    </div>
    <div class="tab-current">
      <div class="miss-wrap">
        <div class="phone clearfix">
          <p>+0086</p>
          <input v-model="mobile" name="mobile" type="tel" placeholder="请输入手机号" />
        </div>
        <div class="code">
          <input
            v-model="msgCode"
            @keyup.enter="submit"
            type="text"
            name="code"
            placeholder="请输入验证码"
          />
          <MsgCode :phone-number="mobile" />
        </div>
        <div class="pwd">
          <input
            v-model="password"
            name="password"
            @keyup.enter="submit"
            :type="activePwd ? 'text' : 'password'"
            placeholder="请输入密码"
          />
          <i
            class="iconfont"
            :class="[activePwd ? 'icon-eye-open' : 'icon-eye-close']"
            @click="activePwd = !activePwd"
          ></i>
        </div>
      </div>
    </div>
    <div class="operate">
      <div class="auto">
        <label @click="toCheck">
          <span class="checkbox" v-show="checked">
            <i class="el-icon-check"></i>
          </span>
          <span class="checkbox" v-show="!checked">&nbsp;</span>
          <span class="text"
            >接收<nuxt-link target="_blank" to="/agreement"
              >《未来校招用户服务协议》</nuxt-link
            >
          </span>
        </label>
      </div>
    </div>
    <div class="submit">
      <button type="button" @click="submit">重设密码</button>
      <p @click="toLogin">已有账号？直接登录</p>
    </div>
  </div>
</template>

<script>
import { resetPassport } from '@/api/users';
import { setUser } from '@/utils/cookie';

export default {
  data () {
    return {
      active: 0,
      checked: false,
      mobile: '',
      msgCode: '',
      password: '',
      activePwd: false,
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    tab (active) {
      this.active = active;
    },
    toCheck () {
      this.checked = !this.checked;
    },
    toLogin () { // 登录
      this.$store.dispatch('user/toggleModal', {
        login: true
      })
    },
    toSetCookie (data) {
      setUser(JSON.stringify(data));
      this.$store.dispatch('user/toggleModal', {})
    },
    handelLogin (res) { // 处理登录
      if (res.code === 200 || res.code === 1003 || res.code === 1004) {
        this.$message.success(res.msg || '提交成功！');
        this.toSetCookie(res.data);
        this.$store.dispatch('user/userInfo', res.data);
      } else {
        this.$message.error(res.msg || '网络错误, 稍后再试！');
      }
    },
    submit () { // 忘记密码
      if (!this.mobile || !this.msgCode || !this.password) {
        this.$message.error('请填写完整！');
        return
      }
      resetPassport({
        mobile: this.mobile,
        code: this.msgCode,
        password: this.password,
      }).then(res => {
        if (res.code === 200) {
          this.handelLogin(res);
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err || '网络错误！')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.miss {
  height: 430px;
  padding: 20px 50px;
  .title {
    height: 50px;
    p {
      line-height: 50px;
      text-align: center;
      font-size: 21px;
      color: #333;
    }
  }
  .miss-wrap {
    margin: 20px 0;
    & > div {
      height: 55px;
      border-bottom: 1px solid #eee;
      input,
      p {
        line-height: 54px;
        height: 54px;
        color: #333;
        font-size: 16px;
      }
    }
    .phone {
      p {
        float: left;
        width: 80px;
      }
    }
    .pwd {
      position: relative;
      i {
        position: absolute;
        right: 0;
        top: 5px;
        font-size: 25px;
        padding: 10px;
        color: #999;
        &:hover {
          cursor: pointer;
          color: #333;
        }
      }
    }
    button {
      float: right;
      margin-top: 10px;
      width: 135px;
      height: 35px;
      border: 1px solid $base-color;
      background: #fff;
      border-radius: 4px;
      color: $base-color;
      &:hover {
        background: $base-color;
        color: #fff;
      }
    }
  }
  .operate {
    .auto {
      label {
        .checkbox {
          display: inline-block;
          height: 22px;
          line-height: 21px;
          width: 22px;
          border: 1px solid #d8d8d8;
          border-radius: 50%;
          text-align: center;
          i {
            vertical-align: text-bottom;
            color: $base-color;
            font-size: 18px;
            font-weight: 600;
          }
        }
        .text,
        a {
          font-size: 16px;
          color: #999;
          margin-left: 5px;
        }
        &:hover {
          .text,
          a {
            color: $base-color;
            cursor: pointer;
          }
        }
      }
    }
  }
  .submit {
    margin-top: 40px;
    text-align: center;
    button {
      height: 45px;
      width: 335px;
      margin: 0 auto;
      background: $base-color;
      color: #fff;
      border: 1px solid $base-color;
      border-radius: 22.5px;
      font-size: 16px;
      &:hover {
        background: #fff;
        color: $base-color;
      }
    }
    p {
      margin-top: 10px;
      color: #999;
      font-size: 16px;
      text-align: center;
      &:hover {
        color: $base-color;
        cursor: pointer;
      }
    }
  }
}
</style>
